package vn.vfriends.vcms.site.service.impl;

import java.util.List;
import javax.annotation.Resource;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.PersistenceUnit;
import javax.persistence.Query;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import javax.transaction.UserTransaction;
import vn.vfriends.vcms.site.model.Category;
import vn.vfriends.vcms.site.service.CategoryService;

/**
 *
 * @author tuan@vfriends.vn
 */
public class CategoryJpaController implements CategoryService {

    @Resource
    private UserTransaction utx = null;
    @PersistenceUnit(unitName = "vcmsPU")
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    @Override
    public List<Category> findCategories() {
        return findCategories(true, -1, -1);
    }

    @Override
    public List<Category> findCategories(int maxResults, int firstResult) {
        return findCategories(false, maxResults, firstResult);
    }

    private List<Category> findCategories(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            cq.select(cq.from(Category.class));
            Query q = em.createQuery(cq);
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    @Override
    public Category findCategory(Long id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(Category.class, id);
        } finally {
            em.close();
        }
    }

    @Override
    public Category findCategoryByName(String name) {
        EntityManager em = getEntityManager();
        try {
            Query query = em.createNamedQuery("Category.findByName", Category.class);
            query.setParameter("name", name);
            List<Category> categoryList = query.getResultList();
            if (categoryList != null && !categoryList.isEmpty()) {
                return categoryList.get(0);
            } else {
                return null;
            }
        } finally {
            em.close();
        }
    }
    
    @Override
    public List<Category> findNoParentCategories() {
        EntityManager em = getEntityManager();
        try {
            CriteriaBuilder cb = em.getCriteriaBuilder();
            CriteriaQuery<Category> criteria = cb.createQuery(Category.class);
            Root<Category> entity = criteria.from(Category.class);
            
            Predicate predicate = cb.conjunction();
            predicate = cb.and(predicate, cb.isNull(entity.get("parent")));
            
            criteria.where(predicate);
            criteria.orderBy(cb.asc(entity.get("orderNumber")));
            Query query = em.createQuery(criteria);
            return query.getResultList();
        } finally {
            em.close();
        }
    }
    
    @Override
    public int getCategoryCount() {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            Root<Category> rt = cq.from(Category.class);
            cq.select(em.getCriteriaBuilder().count(rt));
            Query q = em.createQuery(cq);
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }
    
}
